package com.tengju.bff.interfaces.covercharge.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BeanMapperUtils {

    public static <Source, Target> Target map(Source source,Class<Target> targetClass) {
        if(source == null){
            return null;
        }
        Target data = BeanUtils.instantiate(targetClass);
        BeanUtils.copyProperties(source,data);

        return data;
    }

    public static  <Source, Target> List<Target> mapList(List<Source> sources, Class<Target> targetClass) {
        if(CollectionUtils.isEmpty(sources)){
            return new ArrayList<>(0);
        }

        return sources.stream().map(t -> map(t,targetClass)).collect(Collectors.toList());
    }

}


